﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace CurrentCostGUI
{
    public partial class ConfigForm : Form
    {
        public ConfigForm()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (ValidateForm())
            {
                GuiConfig.SaveUserGuid(txtUserGuid.Text);
            }
        }

        private void ConfigForm_Load(object sender, EventArgs e)
        {
            txtUserGuid.Text = GuiConfig.GetUserGuid();
        }


        private  bool ValidateForm()
        {
            bool ok = false;
            var newGuidText = txtUserGuid.Text;
            Guid guid ;
            ok = IsGuid(newGuidText, out guid);
            

            return ok; 
        }

        private void txtUserGuid_TextChanged(object sender, EventArgs e)
        {
            btnOK.Enabled = ValidateForm();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            //save

            this.Close();
        }


        private static Regex isGuid = new Regex(@"^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$", RegexOptions.Compiled);

        internal static bool IsGuid(string candidate, out Guid output)
        {
            bool isValid = false;
            output = Guid.Empty;
            if (candidate != null)
            {

                if (isGuid.IsMatch(candidate))
                {
                    output = new Guid(candidate);
                    isValid = true;
                }
            }
            return isValid;
        }
    }
}
